<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>订单中心</title>
    <th:block th:include="common/header :: header"/>
</head>
<body>
<div th:replace="common/navbar :: navbar"></div>
<div class="container-fluid p-5">
    <div class="card">
        <div class="card-body">
            <h5 class="card-title">订单列表</h5>
            <div class="table-responsive">
                <table class="table table-bordered table-striped">
                    <thead>
                    <tr>
                        <td>订单号</td>
                        <td>总价格</td>
                        <td>订单时间</td>
                        <td>长期餐</td>
                        <td>详情</td>
                        <td>操作</td>
                    </tr>
                    </thead>
                    <tbody>
                    <tr th:each="order : ${orders}">
                        <td>[[${order.id}]]</td>
                        <td>[[${order.price}]]</td>
                        <td>[[${order.createAt}]]</td>
                        <td>[[${order.longTerm ? "是" : "否"}]]</td>
                        <td>
                            <table class="table table-bordered table-striped">
                                <thead>
                                <tr>
                                    <td>菜名</td>
                                    <td>价格</td>
                                    <td>数量</td>
                                </tr>
                                </thead>
                                <tbody>
                                <tr th:each="menu : ${order.menus}">
                                    <td>[[${menu.name}]]</td>
                                    <td>[[${menu.price}]]</td>
                                    <td>[[${menu.quantity}]]</td>
                                </tr>
                                </tbody>
                            </table>
                        </td>
                        <td>
                            <button th:onclick="goComment([[${order.id}]])" class="btn-primary btn btn-sm">评论</button>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div id="comment" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-body">
                    <button class="close" data-dismiss="modal">
                        <span>×</span>
                    </button>
                </div>
                <div class="modal-title">
                    <h5 class="text-center">评论</h5>
                </div>
                <div class="modal-body">
                    <form class="form-group">
                        <div class="form-group">
                            <label>评论</label>
                            <input name="orderId" class="form-control" hidden="hidden"/>
                            <textarea name="comment" class="form-control" placeholder="请输入评论"></textarea>
                        </div>
                        <div class="text-right">
                            <button class="btn btn-primary" type="button" th:onclick="doComment()">提交</button>
                            <button class="btn btn-danger" data-dismiss="modal">取消</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<th:block th:replace="common/pagenation :: pagination('/order', ${orders.getPageable().getPageNumber()}, ${orders.totalPages}, ${orders.getPageable().getPageSize()})"/>
<th:block th:include="common/scripts :: scripts"/>
<script>
    goComment = (id) => {
        $("input[name='orderId']").val(id)
        $('#comment').modal('show')
    }
    doComment = () => {
        let p = $('#comment form').serializeArray();
        let params = {};
        $.each(p, function() {
            params[this.name] = this.value;
        });
        console.log(params)
        $.ajax({
            type: 'POST',
            url: '/order/comment',
            data: JSON.stringify(params),
            headers: {
                'Content-Type': 'application/json'
            },
            success: res => {
                if (res.code === 0) {
                    toastr.success('评论成功')
                } else if (res.code === -1) {
                    toastr.error(res.message)
                }
            },
            error: err => {
                toastr.error('评论失败')
            },
            complete: () => {
                $('#comment').modal('hide')
            },
            dataType: 'json'
        })
    }
</script>
</body>
</html>